﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Domain.Manufacture;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;
using Yitter.IdGenerator;
namespace RedDragon.RBAC.API.Application.Common.Sale
{
    public class UpdateSaleHandler : IRequestHandler<UpdateSaleCommand, Result>
    {
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 工作表仓储
        /// </summary>
        public readonly IBaseRepository<Sales> SalesRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<UpdateSaleHandler> logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="SalesRepository"></param>
        /// <param name="logger"></param>
        public UpdateSaleHandler(IMapper mapper, IBaseRepository<Sales> SalesRepository, ILogger<UpdateSaleHandler> logger)
        {
            this.mapper = mapper;
            this.SalesRepository = SalesRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 订单修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<Result> Handle(UpdateSaleCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Sales Sales = mapper.Map<Sales>(request);
                var res = await SalesRepository.Update(Sales);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = Sales;
                    result.Msg = "订单修改成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "订单修改失败" + ex.Message;
                logger.LogError("订单修改失败" + ex.Message);
            }
            return result;
        }
    }
}
